<?php
class ModelTicketTicket extends Model {
	public function addTicket($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "ticket SET ticket_subject = '" . $this->db->escape($data['subject']) . "', ticket_status = 'New', ticket_department_id = '" . $this->db->escape($data['department']) . "', ticket_customer_id = '" . $this->db->escape($this->customer->getId()) . "', ticket_created = NOW(), ticket_last_update = '" . (string)$this->customer->getFirstName() . ' ' . $this->customer->getLastName() . "'");
		
		$this->load->language('ticket/create');
		
		$subject = sprintf($this->language->get('message_subject'), $this->config->get('config_name'));
		
		$message  = $this->language->get('message_content');
		
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->parameter = $this->config->get('config_mail_parameter');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');				
		$mail->setTo($this->customer->getEmail());
		$mail->setFrom($this->config->get('config_email'));
		$mail->setSender($this->config->get('config_name'));
		$mail->setSubject($subject);
		$mail->setText($message);
		$mail->send();
		
		return $this->db->getLastId();
	}
	
	public function lastUpdate($ticket_id, $user_name) {
		$this->db->query("UPDATE `" . DB_PREFIX . "ticket` SET ticket_last_update = '" . (string)$user_name . "' WHERE ticket_id = '" . (int)$ticket_id . "'");
	}
				
	public function getTicket($ticket_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "ticket, `" . DB_PREFIX . "department` WHERE ticket_department_id = department_id AND ticket_id = '" . (int)$ticket_id . "'");
		
		return $query->row;
	}
	
	public function getTickets($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "ticket`, `" . DB_PREFIX . "department` WHERE ticket_department_id = department_id AND ticket_customer_id = " . (int)$this->customer->getId();
		
		if ( isset($data['filter_status']) && !empty($data['filter_status']) ){
			$sql .= " AND ticket_status = '" . (string)$data['filter_status'] . "'";
		}
			
		$sort_data = array(
			'ticket_subject',
			'ticket_status',
			'ticket_created'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY ticket_subject";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
}
?>